// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class Egg extends cc.Component {    
    private radian: number = 0; // 弧度
    private isMoving: boolean = false; // 是否要运动
    private a: number = 186 // 长半径
    private b: number = 43; // 短半径
    private clickCallback: any = null; // 点击回调

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_START, event => {
            event.stopPropagation();
        });
    }

    start () {

    }

    update (dt) {
        if(this.isMoving){
            this.radian = this.radian + dt * 5;
            this.updatePos();
        }        
    }

    /**
     * @description: 设置移动
     * @param {type} 
     * @return: 
     */
    setMove(isMoving: boolean){
        this.isMoving = isMoving;
    }

    /**
     * @description: 设置弧度
     * @param {type} 
     * @return: 
     */
    setRadian(radian: number){
        this.radian = radian;
    }

    /**
     * @description: 获取弧度
     * @param {type} 
     * @return: 
     */
    getRadian(){
        return this.radian;
    }

    /**
     * @description: 马上更新位置
     * @param {type} 
     * @return: 
     */
    updatePos(){
        this.node.x = this.a * Math.cos(this.radian);
        this.node.y = this.b * Math.sin(this.radian);
        this.node.scale = 0.9 - Math.sin(this.radian) * 0.1;
        this.node.zIndex = this.node.scale * 100;
    }
}
